soda() 
	._page('iam_department') 
	._module(['dtree', 'sysViews', 'form'])
	._css(['/module/libs/dtree/dtree.css', '/module/libs/dtree/font/dtreefont.css'])
	._api('/v1/iam/department') 
	._query(['deptId^eq^-1']) 
	._orderBy(['createTime^desc']) 
	._run((unit) => { 
		let userVM = null;
		let h = $('#ima_department_toolbar').height() + 'px';
		// 表格基础配置 
		unit.templateTable['id'] = 'ima_department_sub_table';
		unit.templateTable['url'] = unit.api['crud'] + '/users';
		unit.templateTable['height'] = h;
		unit.templateTable['toolbar'] = '<div class="layui-table-tool-temp"><button class="layui-btn layui-btn-sm" lay-event="addDept">添加</button></div>';
		unit.templateTable['cols'] = [ 
			{fixed: 'left', title: '操作', minWidth: 70, toolbar: '<div class="layui-table-cell">'+
			                                '<a class="layui-btn layui-btn-xs" lay-event="removeDept">移出</a>' +
			                             '</div>'},
			{fixed: 'left',field: 'trueName', title: '姓名',minWidth: 120},
			{field: 'mobile', title: '电话',minWidth: 120},
			{field: 'state', title: '状态',templet: function (obj) {
				return obj.state == 1 ? '<span style="color: red;">停用中</span>' : '<span style="color: #00A65A;">使用中</span>';
			}},
		]; 
		unit.templateTable['bar'] = (opt) => { 
			unit.templateDialog[opt.event](); 
		} 
		unit.templateTable['tool'] = (opt) => { 
			unit.templateDialog[opt.event](opt.data); 
		} 
 
		//弹出层 
		unit.templateDialog['addDept'] = (data) => { 
			if(optDeptId == null){
				unit.sdDialog['tips']['error']('请先选择一个部门');
				return false;
			}
			unit.sdDialog.page({
				title: "用户查询",
				btn:[],
				url: '/components/iam/department/users.html',
				init: function (){
				    userVM = $('#dept-query-users-list').vm({
						users:[]
					})
					$('#dept-query-users-list').on('click', '.set-user', function () {
						let uid = $(this).attr('data-id');
						let param = {
							url: unit.api['crud'] + '/users/set',
							data: {userId: uid, departmentId: optDeptId},
							tips: true,
							callback: function (d) {
								render.refresh();
							}
						}
						unit.sdHttp['post'](param);
					})
				}
			});
		} 
		unit.templateDialog['create'] = unit.templateDialog['form'];
		unit.templateDialog['update'] = unit.templateDialog['form'];
		unit.templateDialog['removeDept'] = (data) => {
			unit.sdDialog.confirm({
				title: "是否要将用户移出这个部门?",
				submit() {
					let param = {
 						url: unit.api['crud'] + '/users/set',
 						data: {userId: data.id, departmentId: optDeptId},
 						callback() {
 							render.refresh();
 						}
 					}
 					unit.sdHttp['put'](param);
 				}
 			});
 		}
 		unit.sdTable['render'](unit.templateTable);
		unit.form.on("submit(dept-query-users-submit)", (obj) => {
			let key = obj.field['user-query-key']
			if(key.length > 0){
				let param = {
					url: unit.api['crud'] + '/users/query/key?key=' + key,
					tips: false,
					callback(d){
						userVM.set({
							users: d
						})
					}
				}
				unit.sdHttp['get'](param);
			}
		})
		
		let depts = [], optDeptId = null, optDept = null;
		let render = {
			refresh: function(){
				render.tree();
				let data = {
					deptId: optDeptId
				}
				unit.templateTable['where'] = unit.sdFormat['query'](unit.filter['vo'], data) 
				unit.sdTable['render'](unit.templateTable);
				unit.sdTable.reload('ima_department_sub_table', {
					deptId: optDeptId
				}); 
				render.init();
			},
			init(){
				$('.dtree-layui-item').each(function () {
					let obj = $(this).attr('data-id');
					if(obj == optDeptId){
						$(this).addClass('dtree-layui-item-this')
					}
				})
				unit.dtree.on("node('ima_department_tree')" ,function(obj){
					optDeptId = obj.param.nodeId;
					optDept = obj.param;
					unit.sdTable.reload('ima_department_sub_table', {
						deptId: optDeptId
					}); 
				});
			},
			tree(){
				let param = {
					url: unit.api['list'] + '?limit=1000',
					tips: false,
					isAll: true,
					callback(d){
						dapts = d.list;
						unit.dtree['render']({
							elem: '#ima_department_tree',
							data: d.tree,
							toolbar:true,
							record:true,
							initLevel: 3,
							toolbarScroll:"#ima_department_toolbar",
							toolbarStyle:{
								title: '组织',
								area: ["60%","320px"]
							},
							dot:false,
							skin: "layui",  // layui主题风格
							toolbarBtn: [
								[
									{"label":"排序", "name":"sortId", "type":"text", "value": "0", "placeholder": "数字越大越靠前面"},
								],
								[
									{"label":"排序", "name":"sortId", "type":"text", "value": "0", "placeholder": "数字越大越靠前面"},
								]
							],
							toolbarFun: {
								addTreeNode: function(treeNode, $div){
									treeNode.sortId = unit.sdFormat['convert']['s2n'](treeNode.sortId);
									treeNode.name = treeNode.context;
									let param = {
										url: unit.api['crud'],
										data: treeNode,
										callback: function () {
											render.refresh();
										}
									}
									console.log(param)
									unit.sdHttp['post'](param);
								},
								editTreeLoad: function(treeNode){
									let node = unit.sdFormat['json']['get'](depts, treeNode.nodeId);
									$('input[name="sortId"]').val(node.sortId);
								},
								editTreeNode: function(treeNode, $div){
									let optId = treeNode.nodeId;
									treeNode.sortId = unit.sdFormat['convert']['s2n'](treeNode.sortId);
									treeNode.name = treeNode.context;
									let param = {
										url: unit.api['crud'],
										data: treeNode,
										callback: function () {
											unit.sdTable.reload('ima_department_sub_table', {
												deptId: optDeptId
											}); 
										}
									}
									unit.sdHttp['put'](param, optId);
								},
								delTreeNode: function(treeNode, $div){
									unit.sdDialog.confirm({
										title: "删除后将无法恢复!",
										submit: function () {
											let param = {
												url: unit.api['crud'],
												callback: function () {
													unit.sdTable.reload('ima_department_sub_table', {
														deptId: optDeptId
													}); 
													render.refresh();
												}
											}
											unit.sdHttp['delete'](param, treeNode.nodeId);
										}
									});
								}
							}
						})
						$(document).on('blur', 'input[name="addNodeName"]', function () {
							let val = $(this).val();
							$('input[name="shortName"]').val(val);
						})
						render.init();
					}
				}
				unit.sdHttp['get'](param)
			}
		}
		
		render.tree();
		unit.sysViews['resize']('ima_department_view');
		//$('#ima_department_toolbar').css('height', ($('#ima_department_sub_table_view').height() - 20) + 'px');

 	}); 